﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace WXYW.BLL
{
    public class ExportExcel
    {
        public static void ExportExcelFromGridView(System.Web.UI.WebControls.GridView gv, string excelName)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.HeaderEncoding = Encoding.GetEncoding("GB2312");
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + excelName + ".xls");
            System.Web.HttpContext.Current.Response.Charset = "";
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
            gv.EnableViewState = false;
            //System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CHS", true);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            gv.RenderControl(htmlWrite);

            System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }
    }
}
